package cn.jarlen.android.plugin

import org.gradle.api.Plugin
import org.gradle.api.Project

class ApkPackageHookPlugin implements Plugin<Project> {

    @Override
    void apply(Project project) {
        println("> ApkPgVerPlugin --->Start")
        project.afterEvaluate {
            project.plugins.withId('com.android.application') {
                project.android.applicationVariants.all { variant ->
                    def exeFail
                    try {
                        variant.getMergeAssetsProvider().get().doLast {
                            println("> ApkPgVerPlugin --->getMergeAssetsProvider")
                            def assetsDir = it.outputDir.getAsFile().getOrNull().path
                            def projectRootDir = project.getParent().getBuildDir().getParentFile()
                            writeVer2file(projectRootDir,assetsDir, applicationId, String.valueOf(versionCode), versionName)
                        }
                        exeFail = false
                    } catch (ignored) {
                        println("> ApkPgVerPlugin ---> Compatible with [mergeAssets]")
                        exeFail = true
                    }
                    if (exeFail) {
                        variant.mergeAssets.doLast {
                            println("> ApkPgVerPlugin --->mergeAssets")
                            def assetsDir = it.outputDir.path
                            def projectRootDir = project.getParent().getBuildDir().getParentFile()
                            writeVer2file(projectRootDir,assetsDir, applicationId, String.valueOf(versionCode), versionName)
                        }
                    }
                }
            }
        }
    }

    void writeVer2file(projectRootDir, assetsDir, applicationId, versionCode, versionName) {
        def versionFile = new File(assetsDir, applicationId + '.version')
        if (versionFile.exists()) {
            versionFile.delete()
        }
        versionFile.createNewFile()

        Properties versionProperties = new Properties()
        versionProperties.load(new FileReader(versionFile))
        versionProperties.clear()

        versionProperties.setProperty("application", applicationId)
        versionProperties.setProperty("verCode", versionCode)
        versionProperties.setProperty("verName", versionName)

        VerBuilder verBuilder = VerBuilderFactory.builder(projectRootDir)
        versionProperties.setProperty("builder", verBuilder.getBuilderName())
        versionProperties.setProperty("repUrl", verBuilder.getRepUrl())
        versionProperties.setProperty("repManager", verBuilder.getRepManager())
        versionProperties.setProperty("codeBranch", verBuilder.getCodeBranch())
        versionProperties.setProperty("codeSHA", verBuilder.getCodeSHA())

        versionProperties.store(new FileWriter(versionFile), "ApkPgVerPlugin By Jarlen")
        println("> ApkPgVerPlugin --->Done")
    }

    /**
     * 获取分支名称
     * @return
     */
    def getGitBranch() {
        return 'git symbolic-ref --short -q HEAD'.execute().text.trim()
    }

    def gitGitVersionCode() {
        try {
            return 'git rev-list HEAD --first-parent --count'.execute().text.trim().toInteger()
        }
        catch (ignored) {
            return 1
        }
    }
}